package com.jn.zhinongbao.adapter.adapter;

import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.supportingagriculturaltreasures.R;
import com.example.supportingagriculturaltreasures.bean.News;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.NewsViewHolder> {
    
    private List<News> newsList;
    private OnNewsClickListener listener;

    public interface OnNewsClickListener {
        void onNewsClick(News news);
    }

    public NewsAdapter(OnNewsClickListener listener) {
        this.newsList = new ArrayList<>();
        this.listener = listener;
    }

    @NonNull
    @Override
    public NewsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_news, parent, false);
        return new NewsViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull NewsViewHolder holder, int position) {
        News news = newsList.get(position);
        holder.bind(news);
    }

    @Override
    public int getItemCount() {
        return newsList.size();
    }

    public void updateNews(List<News> newsList) {
        this.newsList.clear();
        if (newsList != null) {
            this.newsList.addAll(newsList);
        }
        notifyDataSetChanged();
    }

    class NewsViewHolder extends RecyclerView.ViewHolder {
        private TextView tvTitle, tvSummary, tvAuthor, tvTime, tvCategory, tvViewCount;
        private ImageView ivImage;

        public NewsViewHolder(@NonNull View itemView) {
            super(itemView);
            tvTitle = itemView.findViewById(R.id.tv_title);
            tvSummary = itemView.findViewById(R.id.tv_summary);
            tvAuthor = itemView.findViewById(R.id.tv_author);
            tvTime = itemView.findViewById(R.id.tv_time);
            tvCategory = itemView.findViewById(R.id.tv_category);
            tvViewCount = itemView.findViewById(R.id.tv_view_count);
            ivImage = itemView.findViewById(R.id.iv_image);

            itemView.setOnClickListener(v -> {
                if (listener != null && getAdapterPosition() != RecyclerView.NO_POSITION) {
                    listener.onNewsClick(newsList.get(getAdapterPosition()));
                }
            });
        }

        public void bind(News news) {
            tvTitle.setText(news.getTitle());
            tvSummary.setText(news.getSummary());
            tvAuthor.setText(news.getAuthor());
            tvTime.setText(news.getCreateTime());
            tvCategory.setText(news.getCategory());
            tvViewCount.setText(String.valueOf(news.getViewCount()));

            // 加载图片 - 统一处理逻辑
            Log.d("NewsAdapter", "Loading image for: " + news.getTitle());
            Log.d("NewsAdapter", "Image URL: " + news.getImageUrl());
            
            if (news.getImageUrl() != null && !news.getImageUrl().isEmpty()) {
                try {
                    File imageFile = new File(news.getImageUrl());
                    Log.d("NewsAdapter", "Image file exists: " + imageFile.exists());
                    Log.d("NewsAdapter", "Image file path: " + imageFile.getAbsolutePath());
                    
                    if (imageFile.exists()) {
                        ivImage.setImageBitmap(BitmapFactory.decodeFile(news.getImageUrl()));
                        ivImage.setVisibility(View.VISIBLE);
                        Log.d("NewsAdapter", "Image loaded successfully");
                    } else {
                        // 如果文件不存在，显示默认图片
                        Log.d("NewsAdapter", "Image file not found, showing default");
                        ivImage.setImageResource(android.R.drawable.ic_menu_gallery);
                        ivImage.setVisibility(View.VISIBLE);
                    }
                } catch (Exception e) {
                    Log.e("NewsAdapter", "Error loading image", e);
                    e.printStackTrace();
                    // 发生异常时也显示默认图片
                    ivImage.setImageResource(android.R.drawable.ic_menu_gallery);
                    ivImage.setVisibility(View.VISIBLE);
                }
            } else {
                Log.d("NewsAdapter", "No image URL provided");
                // 没有图片URL时也显示一个占位图
                ivImage.setImageResource(android.R.drawable.ic_menu_gallery);
                ivImage.setVisibility(View.VISIBLE);
            }
        }
    }
} 